package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"manuscript-service/cache"
	"manuscript-service/model"
	"time"

	"manuscript-service/manurpc/internal/svc"
	"manuscript-service/manurpc/manurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ManuShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewManuShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ManuShowLogic {
	return &ManuShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表查询
func (l *ManuShowLogic) ManuShow(in *manurpc.ManuShowRequest) (*manurpc.ManuShowResponse, error) {
	// todo: add your logic here and delete this line
	var list []*manurpc.ManuList
	key := fmt.Sprintf("show_%v", in.ManuId)
	result, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if result == "" {
		list = model.ManuShow(model.DB, int(in.ManuId), list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Minute*120)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &manurpc.ManuShowResponse{
		List: list,
	}, nil
}
